import  Mock  from "mockjs";

const userList = Mock.mock({
    'list|40':[{
        'id|+1': 1,
        'account': /^1[1-9]\d{12}/,
        'name': '@cname',
        'role|1': ['管理员', '非管理员'],
        'phone': /^1[3-9]\d{9}/,
        'organization|1': ['运营部','研发部','人事部'],
        'status': Mock.Random.boolean
    }]
})

Mock.mock(/\/api\/user\/all/,'get',()=>{

    return {
        code: 200,
        message: 'success',
        data: userList
    }
})

Mock.mock(/\/api\/user\/list/,'get',(options:any)=>{
    const { url } = options
    const query = new URLSearchParams(url.split('?')[1])
    const current = parseInt(query.get('current') || '1')
    const pageSize = parseInt(query.get('pageSize') || '10')
    const start = (current - 1) * pageSize
    const end = current * pageSize
    const total = userList.list.length
    const data = userList.list.slice(start, end )
    return {
        code: 200,
        message: 'success',
        data: {
            list: data,
            total,
            current,
            pageSize
        }
    }
})

Mock.mock(/\/api\/user\/search/, 'get', (options: any) => {
    const { url } = options
    const query = new URLSearchParams(url.split('?')[1])
    const account = query.get('keywords[account]') || ''
    const name = query.get('keywords[name]') || ''
    const phone = query.get('keywords[phone]') || ''
    const current = parseInt(query.get('current') || '1')
    const pageSize = parseInt(query.get('pageSize') || '10')
    //关键字匹配
    const filteredList = userList.list.filter((user: any) => {       
        const fieldsToSearch = [ 'account', 'name', 'phone' ]
        return fieldsToSearch.some(field => {
            const fieldValue = String(user[field] || '').toLowerCase()
            return fieldValue.includes(account.toLowerCase()|| name.toLowerCase()|| phone.toLowerCase())
        })
    })
    const start = (current - 1) * pageSize
    const end = current * pageSize
    const data = filteredList.slice(start, end)
    return {
        code: 200,
        message: 'success',
        data: {
            list: data,
            total: filteredList.length,
            current,
            pageSize
        }
    }
})

Mock.mock(/\/api\/user\/selected/, 'get', (options: any) => {
    const { url } = options
    const query = new URLSearchParams(url.split('?')[1])
    const keywords = query.get('keywords') || ''
    const current = parseInt(query.get('current') || '1')
    const pageSize = parseInt(query.get('pageSize') || '10')
    //关键字匹配
    const filteredList = userList.list.filter((user: any) => {       
        const fieldsToSearch = [ 'organization' ]
        return fieldsToSearch.some(field => {            
            const fieldValue = String(user[field] || '').toLowerCase()
            return fieldValue.includes(keywords.toLowerCase())
        })
    })
    const start = (current - 1) * pageSize
    const end = current * pageSize
    const data = filteredList.slice(start, end)
    if(keywords === 'HY环境科技'){
        return {
            code: 200,
            message: 'success',
            data: {
                list: userList.list.slice(start, end ),
                total: userList.list.length,
                current,
                pageSize
            }
        }
    }
    return {
        code: 200,
        message: 'success',
        data: {
            list: data,
            total: filteredList.length,
            current,
            pageSize
        }
    }
})

Mock.mock(/\/api\/user\/status/, 'get', (options: any) => {
    const { url } = options
    const query = new URLSearchParams(url.split('?')[1])
    const keywords = query.get('keywords') || ''
    const current = parseInt(query.get('current') || '1')
    const pageSize = parseInt(query.get('pageSize') || '10')
    //匹配
    const filteredList = userList.list.filter((user: any) => {       
        const fieldsToSearch = [ 'status' ]
        return fieldsToSearch.some(field => {
            const fieldValue = user[field]
            function parseBoolean(keywords: string):boolean {
                return keywords.toLowerCase() === "true";
            }
            return fieldValue === parseBoolean(keywords)
        })
    })
    const start = (current - 1) * pageSize
    const end = current * pageSize
    const data = filteredList.slice(start, end)
    return {
        code: 200,
        message: 'success',
        data: {
            list: data,
            total: filteredList.length,
            current,
            pageSize
        }
    }
})

Mock.mock(/\/api\/user\/switch/, 'post', (options: any) => {
    const { body } = options
    const userData = JSON.parse(body)
    const index = userList.list.findIndex((user: any) => user.id === userData.id)
    if( index !== -1 ){
        userList.list[index] = {
            ...userList.list[index],
            ...userData,
        }
    }
    return {
        code: 200,
        message: '切换成功',
        data: null
    }
})

Mock.mock(/\/api\/user\/add/, 'post', (options: any) => {
    const { body } = options
    userList.list.push({
        id: userList.list.length + 1,
        ...JSON.parse(body),
    })
    return {
        code: 200,
        message: '添加成功',
        data: null
    }
})

Mock.mock(/\/api\/user\/update/, 'post', (options: any) => {
    const { body } = options
    const userData = JSON.parse(body)
    const index = userList.list.findIndex((user: any) => user.id === userData.id)
    if(index !== -1){
        userList.list[index] = {
            ...userList.list[index],
            ...userData,
        }
    }
    return {
        code: 200,
        message: '更新成功',
        data: null
    }
})

Mock.mock(/\/api\/user\/delete/, 'post', (options: any) => {
    const { url } = options
    const query = new URLSearchParams(url.split('?')[1])
    const id = parseInt(query.get('id') || '') 
    const index = userList.list.findIndex((item: any) => item.id === id)
    if(index !== -1){
        userList.list.splice(index, 1)
    }
    return {
        code: 200,
        message: '删除成功',
        data: null
    }
})


export default Mock